package com;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean
@SessionScoped
public class CommentPage implements Serializable {
	private static final long serialVersionUID = 801021583517122514L;

	private final static String[] users;
	private final static String[] comments;
	private final static Calendar[] dateTimes;

	private String newCommentText;
	private String newCommentUser;

	static {
		users = new String[10];
		users[0] = "Black";
		users[1] = "White";
		users[2] = "Green";
		users[3] = "Red";
		users[4] = "Blue";
		users[5] = "Orange";
		users[6] = "Silver";
		users[7] = "Yellow";
		users[8] = "Brown";
		users[9] = "Maroon";

		comments = new String[10];
		comments[0] = "MercedesMercedesMercedesMercedesMercedes";
		comments[1] = "BMW";
		comments[2] = "Volvo";
		comments[3] = "Audi";
		comments[4] = "Renault";
		comments[5] = "Opel";
		comments[6] = "Volkswagen";
		comments[7] = "Chrysler";
		comments[8] = "Ferrari";
		comments[9] = "Ford";

		dateTimes = new Calendar[10];
		for (int i = 0; i < dateTimes.length; i++) {
			dateTimes[i] = Calendar.getInstance();
			dateTimes[i].add(Calendar.DAY_OF_MONTH, -10 + i);
		}
	}

	private List<Comment> commentList;

	public CommentPage() {
		commentList = new ArrayList<Comment>();
		populateRandomComments(commentList, 4);
	}

	public void newComment() {
	}

	public void submitComment() {
		commentList.add(new Comment("murat", newCommentText, Calendar
				.getInstance().getTime().toString()));
		System.out.println(newCommentText);
		newCommentText = "";
	}

	private void populateRandomComments(List<Comment> list, int size) {
		for (int i = 0; i < size; i++) {
			list.add(new Comment(getRandomUser(), getRandomComment(),
					getNextDateTime()));
		}
	}

	public List<Comment> getCommentList() {
		return commentList;
	}

	private String getRandomUser() {
		return users[(int) (Math.random() * 10)];
	}

	private String getRandomComment() {
		return comments[(int) (Math.random() * 10)];
	}

	private String getNextDateTime() {
		return dateTimes[(int) (Math.random() * 10)].getTime().toString();
	}

	public String getNewCommentText() {
		return newCommentText;
	}

	public void setNewCommentText(String newCommentText) {
		this.newCommentText = newCommentText;
	}

	public String getNewCommentUser() {
		return newCommentUser;
	}

	public void setNewCommentUser(String newCommentUser) {
		this.newCommentUser = newCommentUser;
	}
}